package ro.bluecube.admlocal.gui.users;

import java.util.Collection;
import java.util.Map;

import ro.bluecube.admlocal.delegates.PersonDelegate;
import ro.bluecube.admlocal.delegates.UserDelegate;
import ro.bluecube.admlocal.gui.commons.AbstractController;
import ro.bluecube.admlocal.gui.commons.FormEvent;
import ro.bluecube.admlocal.main.MainFrame;
import ro.bluecube.admlocal.persons.entity.Person;
import ro.bluecube.admlocal.users.entity.User;

public class UsersController extends AbstractController {

	public static final int ACTION_ADAUGARE = 0;

	public static final int ACTION_MODIFICARE = 1;

	public static final int ACTION_STERGERE = 2;
	
	public static final int PERFORM_ADAUGARE = 3;

	UsersForm form;
	UserDelegate userDelegate;

	public void activate(Map context) {
		if (form == null) {
			form = new UsersForm();
			MainFrame.setForm(form);
			form.addFormListener(this);
		}
		form.showForm();
		form.setUsers(getUsers());
	}

	public void perform(FormEvent e) {
		if (e.getID() == ACTION_ADAUGARE || e.getID() == ACTION_MODIFICARE) {
			form.initUpdateUserDialog();
			form.setPersons(getPersons());
		} else if (e.getID() == PERFORM_ADAUGARE) {
			UserDelegate.getInstance().addUser(form.getUpdatedUser());
			form.setUsers(getUsers());
		} else if (e.getID() == ACTION_STERGERE) {
			
		}
	}

	public void updateController(FormEvent e) {

		
	}

	public Collection<User> getUsers() {
		return userDelegate.findAllUsers();
	}
	
	public Collection<Person> getPersons() {
		return new PersonDelegate().findAllPersons();
	}
}
